package com.shrct.businessprocess.process_test.impl;

import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import com.shrct.businessprocess.process_test.PItem;
import com.shrct.businessprocess.process_test.PItemProcess;
import com.shrct.businessprocess.process_test.impl.PItemImpl;
import com.shrct.businessprocess.process_test.proxy.PItemProcessProxy;
import edu.fudan.langlab.domain.organization.Party;
import edu.fudan.langlab.domain.workflow.ActivityNodeDef;
import edu.fudan.langlab.domain.workflow.Token;
import edu.fudan.langlab.domain.workflow.impl.BusinessProcessImpl;
import edu.fudan.mylang.pf.IObjectFactory;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
@DiscriminatorValue("PITEMPROCESS_TYPE")
public class PItemProcessImpl extends BusinessProcessImpl implements PItemProcess {
  public PItemProcessImpl() {
    super();
  }
  
  public String getProcessName() {
    return "修改采购数量申请";
  }
  
  public String getProcessType() {
    return "PItemProcess";
  }
  
  public ActivityNodeDef getActivityDef(final String activityId) {
    edu.fudan.langlab.domain.workflow.ActivityNodeDef result = new edu.fudan.langlab.domain.workflow.ActivityNodeDef();
    result.setActivityId(activityId);
    if("__end__".equals(activityId)){
    	result.setDescription("end");
    }else
    	result.setDescription(Enum.valueOf(com.shrct.businessprocess.process_test.PItemProcessStatus.class, activityId).toString());
    return result;
    
  }
  
  public void _start(final String comment, final PItem pitem, final Integer newCount, final RCEmployee manager, final Token token) {
    this.pitem = pitem;
    this.pitem.setIsPassed(Boolean.valueOf(false));
    this.myProcessManager = manager;
    this.newCount = newCount;
  }
  
  public void start(final String comment, final PItem pitem, final Integer newCount, final RCEmployee manager) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().start(this,com.shrct.businessprocess.process_test.PItemProcessStatus.evaluate.name());
    _start(comment,pitem,newCount,manager,token);
    getProcessManager().registTask(this, token, manager(),getEvaluate_task_message());
    systemExecute();
    String msg = getStart_log_message(comment,pitem,newCount,manager);
    getProcessManager().createActivity(this,"Create Process",msg);
    
    
  }
  
  public String getStart_log_message(final String comment, final PItem pitem, final Integer newCount, final RCEmployee manager) {
    String _plus = (comment + ":");
    String _plus_1 = (_plus + newCount);
    String _name = manager.getName();
    String _plus_2 = (_plus_1 + _name);
    return _plus_2;
  }
  
  public void evaluate() {
  }
  
  public void _evaluate_reject(final String comment, final Token token) {
    Token modify_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.process_test.PItemProcessStatus.modify.name());
    getProcessManager().registTask(this, modify_token,sale(),getModify_task_message());
  }
  
  public void evaluate_reject(final String comment) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.process_test.PItemProcessStatus.evaluate.name());
    _evaluate_reject(comment,token);
    systemExecute();
    String msg = getEvaluate_Reject_log_message(comment);
    getProcessManager().createActivity(this,"上级审批.拒绝",msg);
    
    
  }
  
  public String getEvaluate_Reject_log_message(final String comment) {
    return "";
  }
  
  public void _evaluate_agree(final Token token) {
    this.pitem.setItemCount(this.newCount);
    this.pitem.setIsPassed(Boolean.valueOf(true));
    Token __end___token = getProcessManager().forwardTo(token,"__end__");
    
  }
  
  public void evaluate_agree() {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.process_test.PItemProcessStatus.evaluate.name());
    _evaluate_agree(token);
    systemExecute();
    String msg = getEvaluate_Agree_log_message();
    getProcessManager().createActivity(this,"上级审批.同意",msg);
    
    
  }
  
  public String getEvaluate_Agree_log_message() {
    return "";
  }
  
  public String getEvaluate_task_message() {
    return "";
  }
  
  public void _modify(final String comment, final Integer newCount, final Token token) {
    this.newCount = newCount;
    Token evaluate_token = getProcessManager().forwardTo(token,com.shrct.businessprocess.process_test.PItemProcessStatus.evaluate.name());
    getProcessManager().registTask(this, evaluate_token,manager(),getEvaluate_task_message());
  }
  
  public void modify(final String comment, final Integer newCount) {
    edu.fudan.langlab.domain.workflow.Token token = getProcessManager().assertToken(this,com.shrct.businessprocess.process_test.PItemProcessStatus.modify.name());
    _modify(comment,newCount,token);
    systemExecute();
    String msg = getModify_log_message(comment,newCount);
    getProcessManager().createActivity(this,"销售修改申请",msg);
    
  }
  
  public String getModify_log_message(final String comment, final Integer newCount) {
    return comment;
  }
  
  public String getModify_task_message() {
    return "";
  }
  
  @JoinColumn(name = "pitem_id")
  @ManyToOne(targetEntity = PItemImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private PItem pitem;
  
  public PItem getPitem() {
    return this.pitem;
  }
  
  public PItemProcess setPitem(final PItem pitem) {
    this.pitem = pitem;
    return this;			
    
  }
  
  private Integer newCount;
  
  public Integer getNewCount() {
    return this.newCount;
  }
  
  public PItemProcess setNewCount(final Integer newCount) {
    this.newCount = newCount;
    return this;			
    
  }
  
  @JoinColumn(name = "myProcessManager_id")
  @ManyToOne(targetEntity = RCEmployeeImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCEmployee myProcessManager;
  
  public RCEmployee getMyProcessManager() {
    return this.myProcessManager;
  }
  
  public PItemProcess setMyProcessManager(final RCEmployee myProcessManager) {
    this.myProcessManager = myProcessManager;
    return this;			
    
  }
  
  public boolean _systemExecute() {
    boolean stateChanged = false;
    return stateChanged;
  }
  
  public static PItemProcess create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.process_test.PItemProcess pItemProcess = new com.shrct.businessprocess.process_test.impl.PItemProcessImpl();
    objectFactory.create(pItemProcess);
    return pItemProcess;			
    
  }
  
  public Party sale() {
    RCEmployee _saler = this.pitem.getSaler();
    return _saler;
  }
  
  public Party manager() {
    return this.myProcessManager;
  }
  
  public PItemProcessProxy toProxy() {
    com.shrct.businessprocess.process_test.proxy.PItemProcessProxy proxy = new com.shrct.businessprocess.process_test.proxy.PItemProcessProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
